package com.songshuai.sales.tool.service.impl;

import com.songshuai.sales.tool.constant.SalesToolConstant;
import com.songshuai.sales.tool.dao.tool.LabelRelationMapper;
import com.songshuai.sales.tool.dto.request.label.AddLabelReqDto;
import com.songshuai.sales.tool.dto.request.label.UpdateLabelReqDto;
import com.songshuai.sales.tool.dto.response.label.QueryLabelsPageResDto;
import com.songshuai.sales.tool.dto.response.label.QueryLabelsResDto;
import com.songshuai.sales.tool.service.LabelService;
import com.songshuai.webapi.baseapi.constant.BaseConstant;
import com.songshuai.webapi.baseapi.model.sales.Label;
import com.songshuai.sales.tool.dao.tool.LabelMapper;

import com.songshuai.webapi.baseapi.utils.PropertiesUtil;
import com.songshuai.webapi.baseapi.utils.SnowFlakeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 此类为[基础代码]自动生成,已经拥有基本的增删改成操作 
 * <其他请自行扩展>
 * @Author: 刘汉轩
 * @Date: 2019-03-19 14:49:38
 * 
 */
@Service
public class LabelServiceImpl extends BaseServiceImpl implements LabelService{
 
      
    @Autowired
	private LabelMapper labelMapper;

    @Autowired
    private SnowFlakeUtil snowFlakeUtil;

    @Autowired
    private LabelRelationMapper labelRelationMapper;
	
	/**
     * 新增
     * @param entity 
     */
    public int insertSelective (Label entity) { 
        return this.labelMapper.insertSelective(entity);
    }	
    
    /**
     * 批量新增
     * @param list 
     */
    public void insertBatch(List<Label> list) {       
        this.labelMapper.insertBatch(list); 
    }
    
    /**
     * 根据主键修改单条数据
     * @param entity 
     */
    public int updateByPrimaryKey(Label entity) {
        return this.labelMapper.updateByPrimaryKey(entity);
    }
    
    /**
     * 根据主键批量修改数据
     * @param list 
     */
    public void updateBatch(List<Label> list) {      
        this.labelMapper.updateBatch(list);     
    }
    
    /**
     * 根据主键查询单条数据
     * @param labelId
     */
    public Label selectByPrimaryKey(String labelId) {
        return this.labelMapper.selectByPrimaryKey(labelId);
    }
    
    /**
     * 根据条件查询单条记录
     * @param entity 
     */
    public Label selectByEntity(Label entity) {
        return this.labelMapper.selectByEntity(entity);
    }
      
    /**
     * 根据条件查询多条记录
     * @param entity 
     */
    public List<Label> selectByEntityList(Label entity) {
        return this.labelMapper.selectByEntityList(entity);
    }
    
    /**
     * 根据条件查询Id
     * @param entity 
     */
    public String selectById(Label entity) {
        return this.labelMapper.selectById(entity);
    }
    
    /**
     * 根据条件查询Ids
     * @param entity 
     */
    public List<String> selectByIds(Label entity) {
        return this.labelMapper.selectByIds(entity);
    }
    
    /**
     * 根据主键删除单条记录
     * @param labelId
     */
    public void deleteByPrimaryKey(String labelId) {
        this.labelMapper.deleteByPrimaryKey(labelId);
    }
    
    /**
     * 根据主键删除多条记录
     * @param list
     */
    public void deleteBatch(List<String> list) {
        this.labelMapper.deleteBatch(list);
    }
    
    /**
     * 根据某些条件删除
     * @param entity
     */
    public void deleteByEntity(Label entity) {
        this.labelMapper.deleteByEntity(entity);
    }

    /**
     * 根据标签名分页查询
     * @param labelName
     * @return
     */
    @Override
    public List<QueryLabelsPageResDto> queryLabelsByName(String labelName) {
        return this.labelMapper.queryLabelsByName(labelName);
    }


    /**
     * 插入标签
     * @param request
     * @return
     */
    @Override
    public boolean insertLabel(AddLabelReqDto request) {
        Date date = new Date();
        Label label = new Label();
        PropertiesUtil.copyProperties(request,label);
        String nextId = snowFlakeUtil.nextId();
        label.setLabelId(nextId);
        label.setParentId(StringUtils.isNotBlank(request.getParentId())?request.getParentId(): SalesToolConstant.LABEL_PARENT_ID);
        label.setCreateTime(date);
        label.setUpdateTime(date);
        String userId = getCurrentUser().getUserId();
        label.setCreateBy(userId);
        label.setUpdateBy(userId);
        labelMapper.insertSelective(label);
        return true;
    }


    /**
     * 查询标签组下子标签关联数量
     * @param labelId
     * @return
     */
    public int queryRelationSopCount(String labelId){
        return labelMapper.queryRelationSopCount(labelId);
    }



    /**
     * 修改标签信息
     * @param request
     * @return
     */
    public boolean updateLabel(UpdateLabelReqDto request){
        Label label = new Label();
        PropertiesUtil.copyProperties(request,label);
        label.setUpdateTime(new Date());
        label.setUpdateBy(getCurrentUser().getUserId());
        return labelMapper.updateByPrimaryKey(label) > 0;
    }


    /**
     * 根据id删除标签
     * @param labelId
     * @return
     */
    public boolean deleteLabelById(String labelId){
        //修改标签删除标识
        labelMapper.updateByPrimaryKey(new Label(){{
            setLabelId(labelId);
            setIsDelete(BaseConstant.IS_DELETE_FLAG_YES);
        }});
        return true;
    }


    /**
     * 根据id查询子标签集合
     * @param labelId
     * @return
     */
    @Override
    public List<QueryLabelsResDto> queryLabels(String labelId) {
        List<Label> labels = labelMapper.queryLabels(labelId);
        if (!CollectionUtils.isEmpty(labels)){
            return labels.stream().map(label -> {
                QueryLabelsResDto resDto = new QueryLabelsResDto();
                PropertiesUtil.copyProperties(label,resDto);
                return resDto;
            }).collect(Collectors.toList());
        }
        return null;
    }


}